<template>
  <div>
    <h1>城市管理</h1>
    <br>
    <el-row>
      <el-col :span="14">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="省会名称">
            <el-input v-model="formInline.provinceName" placeholder="省会名称（模糊查询）"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>
        </el-form>

      </el-col>
      <el-col :span="10">
        <el-button type="warning" @click="addCityEdit" style="float:right;margin-left: 30px;">新增城市
        </el-button>
      </el-col>
      <el-col :span="24">
        <el-table
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%">
          <el-table-column
              prop="provinceName"
              label="省会名称">
          </el-table-column>
          <el-table-column
              prop="cityName"
              label="城市名称">
          </el-table-column>
          <el-table-column
              prop="latitude"
              label="经度">
          </el-table-column>
          <el-table-column
              prop="longitude"
              label="纬度">
          </el-table-column>
          <el-table-column
              prop="zoom"
              label="缩放级别">
          </el-table-column>
          <el-table-column
              fixed="right"
              label="操作"
              width="150px">
            <template v-slot="scope">
              <el-button @click="updateCityBtn(scope.row.id)" size="small">修改</el-button>
              <el-button @click="deleteCityBtn(scope.row.id)" type="danger" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>

      <el-col :span="24" style="margin-top: 20px;text-align: center">
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </el-col>
    </el-row>
    <el-dialog title="城市编辑" :visible.sync="dialogFormVisible">
      <CityEdit v-if="dialogFormVisible" :id="editId" @close-dialog="closeDialog">

      </CityEdit>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";
import CityEdit from "@/components/pages/CityEdit";
export default {
  name: "CityPage",
  components: {CityEdit},
  data() {
    return {
      formInline: {
        provinceName: '',
      },
      editId: 0,
      dialogFormVisible: false,
      // 用户数据
      tableData: [],
      // 模糊搜索
      input: "",
      // 分页的 pageSize total currentPage
      pageSize: 10,
      total: 0,
      currentPage: 1,
    }
  },
  created() {
    this.loadCity()
  },

  methods: {
    async loadCity() {
      let data = await request("city?page=" + this.currentPage + "&size=" + this.pageSize + "&provinceName=" + this.formInline.provinceName);
      this.tableData = data.list
      this.total = data.total
    },

    addCityEdit() {
      this.editId = 0
      this.dialogFormVisible = true;
    },
    updateCityBtn(id) {
      this.dialogFormVisible = true
      this.editId = id;
    },
    // 分页
    handleSizeChange(i) {
      this.pageSize = i;
      this.currentPage = 1;
      this.loadCity()
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.loadCity()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 删除city按钮
    deleteCityBtn(id) {
      this.$confirm('此操作将永久禁用该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        request(`city?id=${id}`, "DELETE").then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.loadCity()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    closeDialog(b) {
      this.dialogFormVisible = false;
      if (b) {
        this.loadCity()
      }
    },
    onSubmit() {
      this.loadCity()
    },
  }
}
</script>

<style scoped>

</style>